using UnityEngine;
using System.Collections;

public class MovingCamera : MonoBehaviour {
	
	public Camera FixedCamera;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		//use "q" or "w" key to scale up or down the view
		if (Input.GetKeyDown(KeyCode.Q)) 
		{
			Camera.main.transform.position += Camera.main.transform.forward;
		}
		if (Input.GetKeyDown(KeyCode.W)) 
		{
			Camera.main.transform.position -= Camera.main.transform.forward;
		}
		
		//get the bounds(left/right/up/bottem) of the screen
		float CameraToScreen = Mathf.Abs(FixedCamera.transform.position.z-transform.position.z);
		float LeftBound = FixedCamera.ScreenToWorldPoint(new Vector3(0,0,CameraToScreen)).x;
		float RightBound = FixedCamera.ScreenToWorldPoint(new Vector3(FixedCamera.pixelWidth,FixedCamera.pixelHeight,CameraToScreen)).x;
		
		float UpperBound = FixedCamera.ScreenToWorldPoint(new Vector3(FixedCamera.pixelWidth,FixedCamera.pixelHeight,CameraToScreen)).y;
		float LowerBound = FixedCamera.ScreenToWorldPoint(new Vector3(0,0,CameraToScreen)).y;
		
		//if the Ship go beyond the bound, it will appear on the other side of the screen
		Vector3 pos = transform.position;
		
		if (pos.x <= LeftBound) pos.x = RightBound - LeftBound + pos.x;
		if (pos.x >= RightBound) pos.x = LeftBound + pos.x - RightBound;
		
		if (pos.y <= LowerBound) pos.y = UpperBound - LowerBound + pos.y;
		if (pos.y >= UpperBound) pos.y = LowerBound + pos.y - UpperBound;
		
		transform.position = pos;
		
		//Debug.Log(transform.position);
	}
}
